package org.projectback.utils;


import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import lombok.Data;
@Data
public class ResultMessage<T> {
    private String message;
    private T data;
    private int code;
    private ResultMessage(int code, T data,String message) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
    public static<T> ResultMessage<T> success(int code,T data) {
        return new ResultMessage<>(code, data, "Success!");
    }

    public static<T> ResultMessage<T> failure(int code, String message) {
        return new ResultMessage<>(code,null,message);
    }
    public static<T> ResultMessage<T> unauthorized() {
        return failure(401,"No authorization!");
    }
    public static<T> ResultMessage<T> forbidden() {
        return failure(403, "Forbidden!");
    }

    public String toJsonString() {
        return JSONObject.toJSONString(this, JSONWriter.Feature.WriteMapNullValue);
    }
}
